package com.jiangjiang.eshop.inventory.updater;

import com.jiangjiang.eshop.common.help.DateHelper;
import com.jiangjiang.eshop.inventory.domain.GoodsStockEntity;
import com.jiangjiang.eshop.inventory.service.InventoryGoodsStockService;
import com.jiangjiang.eshop.order.dto.OrderItemDTO;

import java.util.List;
import java.util.Map;

/**
 * @ClassName SubmitOrderStockUpdater
 * @Description 提交订单updater
 * @Author jhw
 * @Date 2021/6/14 13:05
 * @Version 1.0
 **/
public class SubmitOrderStockUpdater extends AbstractStockUpdater {

    private Map<Long, OrderItemDTO> orderItemDTOMap;

    public SubmitOrderStockUpdater(
            InventoryGoodsStockService goodsStockService,
            List<GoodsStockEntity> goodsStockEntities,
            DateHelper dateHelper, Map<Long, OrderItemDTO> orderItemDTOMap) {
        super(goodsStockService, goodsStockEntities, dateHelper);
        this.orderItemDTOMap = orderItemDTOMap;
    }

    @Override
    protected void updateSaleStockQuantity() throws Exception {
        for (GoodsStockEntity goodsStockEntity : goodsStockEntities) {
            OrderItemDTO orderItemDTO = orderItemDTOMap.get(goodsStockEntity.getGoodsSkuId());
            goodsStockEntity.setSaleStockQuantity(
                    goodsStockEntity.getSaledStockQuantity() - orderItemDTO.getPurchaseQuantity()
            );
        }
    }

    @Override
    protected void updateLockStockQuantity() throws Exception {
        for (GoodsStockEntity goodsStockEntity : goodsStockEntities) {
            OrderItemDTO orderItemDTO = orderItemDTOMap.get(goodsStockEntity.getGoodsSkuId());
            goodsStockEntity.setSaleStockQuantity(
                    goodsStockEntity.getSaledStockQuantity() + orderItemDTO.getPurchaseQuantity()
            );
        }
    }

    @Override
    protected void updateSoldStockQuantity() throws Exception {

    }
}
